
function NewWs(url: string,
    open: (ev: Event) => any,
    error: (ev: Event) => any,
    close: (ev: CloseEvent) => any,
    message: (ev: MessageEvent) => any
): WebSocket {
    let ws = new WebSocket(
        url
    );
    ws.onopen = function (this: WebSocket, ev: Event): any {
        // 函数的第一个参数是 this 时，调用的时候可以不传
        return open(ev)
    }

    ws.onerror = function (this: WebSocket, ev: Event): any {
        return error(ev)
    }
    ws.onclose = function (this: WebSocket, ev: CloseEvent): any {
        return close(ev)
    }
    // 3.服务器每次返回信息都会执行一次onmessage方法
    ws.onmessage = function (this: WebSocket, ev: MessageEvent): any {
        return message(ev)
        //  {"messageId":1,"type":1,"simpleMessage":{"fromId":4,"toId":5,"content":"撒打算大的你好","type":1},"groupMessage":{"groupId":0,"senderId":0,"content":"","type":0,"atId":0},"loginMessage":{"userId":0,"userName":"","password":""}}
    };
    return ws;
}

export { NewWs };